package controller;

import model.User;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserChangePwd", value = "/user_changepwd")
public class UserChangePwdServlet extends HttpServlet {
    UserService us = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;Charset=utf-8");
        String password = request.getParameter("password");
        String newPwd = request.getParameter("newPassword");
        User u = (User) request.getSession().getAttribute("user");
        if (password.equals(u.getPassword())) {
            u.setPassword(newPwd);
            us.updatePwd(u);
            request.setAttribute("msg", "修改成功！");
        } else {
            request.setAttribute("failMsg", "修改失败，原密码不正确，你再想想！");
        }
        request.getRequestDispatcher("user_center.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
